package com.lh.aliyunOss.service.Impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.lh.aliyunOss.config.AliyunOssConfig;
import com.lh.aliyunOss.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FileServiceimpl implements FileService {

    @Autowired
    private AliyunOssConfig aliyunOssConfig;

    private String endpoint;
    private String keyid;
    private String keysecret;
    private String bucketname;

    private OSS ossClient;

    public FileServiceimpl(AliyunOssConfig aliyunOssConfig) {
        this.endpoint = aliyunOssConfig.getEndpoint();
        this.keyid = aliyunOssConfig.getKeyid();
        this.keysecret = aliyunOssConfig.getKeysecret();
        this.bucketname = aliyunOssConfig.getBucketname();
    }
    //    FileServiceimpl(){
//        endpoint=aliyunOssConfig.getEndpoint();
//        keyid=aliyunOssConfig.getKeyid();
//        keysecret=aliyunOssConfig.getKeysecret();
//        bucketname=aliyunOssConfig.getBucketname();
//        System.out.println(endpoint);
//        System.out.println(keyid);
//    }

    @Override
    public String upload(InputStream inputStream, String module, String originalFilename) {
//        endpoint=aliyunOssConfig.getEndpoint();
//        keyid=aliyunOssConfig.getKeyid();
//        keysecret=aliyunOssConfig.getKeysecret();
//        bucketname=aliyunOssConfig.getBucketname();
        // 创建OSSClient实例。
        ossClient=new OSSClientBuilder().build(endpoint,keyid,keysecret);

        //判断bucket是否存在，不存在就创建
        if(!ossClient.doesBucketExist(bucketname)){
            ossClient.createBucket(bucketname);
            //设置权限，公共的
            ossClient.setBucketAcl(bucketname,CannedAccessControlList.PublicRead);
        }

        // 创建PutObjectRequest对象。
        //PutObjectRequest putObjectRequest=new PutObjectRequest(module,originalFilename,inputStream);

        // 上传时设置存储类型与访问权限
        //ObjectMetadata metadata = new ObjectMetadata();
        //metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        //metadata.setObjectAcl(CannedAccessControlList.PublicRead);
        //putObjectRequest.setMetadata(metadata);

        //构建文件夹名称
        String folder = new DateTime().toString("yyyy/MM/dd");
        String fileName=UUID.randomUUID().toString();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String objName=module+"/"+folder+"/"+fileName+fileExtension;

        // 上传文件。
        ossClient.putObject(bucketname,objName,inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        return "https://"+bucketname+"."+endpoint+"/"+objName;
    }

    @Override
    public void remove(String url) {
        log.info("endpoint="+endpoint+",keyid="+keyid+",keysecret="+keysecret+",bucketname="+bucketname);

        //创建oss实例
        OSS ossClient=new OSSClientBuilder().build(endpoint,keyid,keysecret);

        //删除文件
        String host="https://"+bucketname+"."+endpoint+"/";
        String objctName=url.substring(host.length());
        ossClient.deleteObject(bucketname,objctName);

        //关闭oss实例
        ossClient.shutdown();

    }

    @Override
    public void removeList(List<String> urls) {
        log.info("endpoint="+endpoint+",keyid="+keyid+",keysecret="+keysecret+",bucketname="+bucketname);

        //创建oss实例
        OSS ossClient=new OSSClientBuilder().build(endpoint,keyid,keysecret);

        //删除文件
        String host="https://"+bucketname+"."+endpoint+"/";
        List<String> urlList = urls.stream().map(item -> item.substring(host.length())).collect(Collectors.toList());
        ossClient.deleteObjects(new DeleteObjectsRequest(bucketname).withKeys(urlList));

        //关闭oss实例
        ossClient.shutdown();
    }
}
